package com.hhkj.study.admin.resource.controller;

import com.hhkj.study.admin.resource.entity.ResourceCourseLive;
import com.hhkj.study.admin.resource.excel.ResourceCourseLiveExcelRes;
import com.hhkj.study.admin.resource.excel.ResourceLivePlayBackExcelRes;
import com.hhkj.study.admin.resource.excel.ResourceQuestionBankExcelRes;
import com.hhkj.study.admin.resource.model.req.ResourceCourseLiveReq;
import com.hhkj.study.admin.resource.model.res.ResourceCourseLiveRes;
import com.hhkj.study.admin.resource.service.biz.ResourceCourseLiveBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v3.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.ExcelImportBaseTemplate;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import com.ruoyi.common.valid.VDisable;
import com.ruoyi.common.valid.VEdit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* ResourceCourseLiveController
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@RestController
@PermissionPrefix("resource:resourceCourseLive")
@RequestMapping("/admin/resource/course/live")
public class ResourceCourseLiveController extends BaseController<ResourceCourseLive, ResourceCourseLiveReq, ResourceCourseLiveRes, ResourceCourseLiveBizService> {

    /** 上下架 */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @PutMapping("/publish")
    public AjaxResult updatePublishFlag(@Validated(value = {VEdit.class, VDisable.class}) @RequestBody ResourceCourseLiveReq req) {
        return toAjax(this.getBizService().updatePublishFlag(req));
    }

    /**
     * 导出
     * @param req
     * @param response
     */
    @PreAuthorize("@ss.hasPermi(this, ':export')")
    @PostMapping("/export")
    public void export(@RequestBody ResourceCourseLiveReq req, HttpServletResponse response) {
        try {
            getBizService().export(req, response);
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            log.error("导出文件失败", e);
            throw new GlobalException("系统异常，文件导出失败，请稍后重试");
        }
    }

    /**
     * 导入
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, ':import')")
    @PostMapping("/import")
    public AjaxResult importCourseLive(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return AjaxResult.success(getBizService().importCourseLive(file, request));
    }

    /**
     * 批量导入回放
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, ':import')")
    @PostMapping("/batch/import")
    public AjaxResult batchImportPlayBack(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return AjaxResult.success(getBizService().batchImportPlayBack(file, request));
    }

    /**
     * 导入回放模板下载
     */
    @PostMapping("/playback/download")
    public void playbackDownload(HttpServletResponse response) {
        try {
            ExcelUtils.downloadExcelTemplateRuoYi(response, ResourceLivePlayBackExcelRes.class, new ResourceLivePlayBackExcelRes());
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("系统异常，模板生成失败，请稍后重试");
        }
    }

    @Override
    public ExcelImportBaseTemplate getExcelImportBaseTemplate() {
        return new ResourceCourseLiveExcelRes();
    }
}
